/*
 * Copyright (c) 2014 - 2016. Danlu.com Co.Ltd. All rights reserved.
 */

package cn.com.dhc.danlu.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CountDownTimer;

/**
 * author: 吴海洋(<a href="mailto:wuhaiyang@danlu.com">wuhaiyang@danlu.com</a>)<br/>
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2016-07-07 16:24<br/>
 *
 * <p>
 * 带倒计时的按钮
 * </p>
 */
@SuppressLint("AppCompatCustomView")
public class CountTimerButton extends Button implements View.OnClickListener {

    private OnInnerClickListener mInnerClickListener;

    private String reAppendText; // 倒计时结束
    private String appendTickingText; // 倒计时进行


    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @SuppressLint("SetTextI18n")
        @Override
        public void onTick(long millisUntilFinished) {
            setText(millisUntilFinished / 1000 + appendTickingText);
        }

        @Override
        public void onFinish() {
            setText(reAppendText);
            setEnabled(true);
            if (null != mInnerClickListener) {
                mInnerClickListener.onCountFinish();
            }
        }
    };

    public CountTimerButton(Context context) {
        this(context, null);
    }

    public CountTimerButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CountTimerButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CountTimerButton);
        reAppendText = array.getString(R.styleable.CountTimerButton_reAppendText);
        appendTickingText = array.getString(R.styleable.CountTimerButton_appendTickingText);
        if (TextUtils.isEmpty(reAppendText)) {
            reAppendText = getResources().getString(R.string.register_verify_code);
        }
        if (TextUtils.isEmpty(appendTickingText)) {
            appendTickingText = "秒";
        }
        array.recycle();
        setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (null != mInnerClickListener) {
            mInnerClickListener.onClick(v);
        }
    }

    /**
     * 启动计时器
     */
    public void startTimer() {
        // 请求服务器获取短信验证码
        // 开启定时器60
        timer.cancel();
        timer.start();
        setEnabled(false);
    }

    /**
     * 暂停计时器
     */
    public void stopTimer() {
        timer.cancel();
        setEnabled(true);
        setText(reAppendText);
    }

    public void setInnerClickListener(OnInnerClickListener innerClickListener) {
        mInnerClickListener = innerClickListener;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        timer.cancel();
    }

    public interface OnInnerClickListener {
        void onClick(View view);

        /**
         * 秒表计时完成
         */
        void onCountFinish();
    }
}